<?php
namespace app\command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateAll extends Command
{
    protected static $defaultName = 'make:generate-all';
    
    protected function configure()
    {
        $this->setDescription('Generate all modules code');
    }
    
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            // 获取所有模块配置文件
            $moduleFiles = glob(base_path() . '/modules/*.php');
            
            if (empty($moduleFiles)) {
                $output->writeln("<error>No module configuration files found in /modules directory.</error>");
                return self::FAILURE;
            }
            
            $output->writeln("<info>Found " . count($moduleFiles) . " modules to generate.</info>\n");
            
            foreach ($moduleFiles as $file) {
                $moduleName = basename($file, '.php');
                $output->writeln("<comment>Generating module: {$moduleName}</comment>");
                
                // 创建生成器实例并执行
                $generator = new Generator();
                $generator->generate($moduleName, $output);
                
                $output->writeln('');
            }
            
            $output->writeln("\n<info>All modules generated successfully!</info>");
            
            return self::SUCCESS;
        } catch (\Exception $e) {
            $output->writeln("<error>Error: {$e->getMessage()}</error>");
            return self::FAILURE;
        }
    }
} 